#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/tests/stats"

stats = LKP::Stats.new

results = {}
leaking_number = 0

while (line = $stdin.gets)
  case line
  when /^\[\d+ modules\] .+(Live|Loading|Unloading) .+/
    # skip the line about kernel modules
    next
  when /addr:.*vm_flags:.*anon_vma:.*mapping:.*index:/
    # [  223.066648 ] addr:00005586d8f2b000 vm_flags:00000075 anon_vma:0000000000000000 mapping:ffff88810b5aaa98 index:3b
    next
  when /Total number of results.*: (.*)/
    total_number = $1
  when /^\[ +([\d.]+)\] (.*)/
    # [    2.205829] mapped IOAPIC to ffffffffff5fb000 (fec00000)
    results["tcase.dmesg.#{$2.sub!(/\b(0x)?ffff[[:xdigit:]]{12}\b/, '').delete(' ')}"] = 1
    leaking_number += 1
  when /^\[\d+ ([^\d])([^\]]+)\](.*)/
    results["tcase.proc.#{$1}#{$2}.#{$3.sub!(/\b(0x)?ffff[[:xdigit:]]{12}\b/, '').gsub(/\s+/, '')}"] = 1
    leaking_number += 1
  end
end

if results.empty?
  results['tcase.pass'] = 1
else
  results['eid.fail'] = 1
end

results['ttotal.leaking_number'] = leaking_number
results['ttotal.total_number'] = total_number.to_i

results.each do |k, v|
  stats.add_id k, v
end

stats.dump
